package com.android.settingslib.datetime;

import android.content.Context;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class ZoneGetter {

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_GMT = "gmt";

    public static List<Map<String, Object>> getZonesList(Context context) {
        List<Map<String, Object>> zonesList = new ArrayList<>();

        String[] timeZoneIds = {
            "Pacific/Midway", "Pacific/Honolulu", "America/Anchorage", "America/Los_Angeles",
            "America/Tijuana", "America/Phoenix", "America/Chihuahua", "America/Denver",
            "America/Costa_Rica", "America/Chicago", "America/Mexico_City", "America/Regina",
            "America/Bogota", "America/New_York", "America/Caracas", "America/Barbados",
            "America/Halifax", "America/Manaus", "America/Santiago", "America/St_Johns",
            "America/Recife", "America/Sao_Paulo", "America/Argentina/Buenos_Aires",
            "America/Godthab", "America/Montevideo", "Atlantic/South_Georgia", "Atlantic/Azores",
            "Atlantic/Cape_Verde", "Etc/UTC", "Africa/Casablanca", "Europe/London",
            "Europe/Amsterdam", "Europe/Belgrade", "Europe/Brussels", "Europe/Madrid",
            "Europe/Sarajevo", "Africa/Windhoek", "Africa/Brazzaville", "Asia/Amman",
            "Europe/Athens", "Europe/Istanbul", "Asia/Beirut", "Africa/Cairo", "Europe/Helsinki",
            "Asia/Jerusalem", "Europe/Minsk", "Africa/Harare", "Asia/Baghdad", "Europe/Moscow",
            "Asia/Kuwait", "Africa/Nairobi", "Asia/Tehran", "Asia/Baku", "Asia/Tbilisi",
            "Asia/Yerevan", "Asia/Dubai", "Asia/Kabul", "Asia/Karachi", "Asia/Oral",
            "Asia/Yekaterinburg", "Asia/Kolkata", "Asia/Colombo", "Asia/Kathmandu", "Asia/Almaty",
            "Asia/Yangon", "Asia/Krasnoyarsk", "Asia/Bangkok", "Asia/Jakarta", "Asia/Shanghai",
            "Asia/Hong_Kong", "Asia/Irkutsk", "Asia/Kuala_Lumpur", "Australia/Perth",
            "Asia/Taipei", "Asia/Seoul", "Asia/Tokyo", "Asia/Yakutsk", "Australia/Adelaide",
            "Australia/Darwin", "Australia/Brisbane", "Australia/Hobart", "Australia/Sydney",
            "Asia/Vladivostok", "Pacific/Guam", "Asia/Magadan", "Pacific/Noumea",
            "Pacific/Majuro", "Pacific/Auckland", "Pacific/Fiji", "Pacific/Tongatapu",
            "Europe/Berlin", "Europe/Paris", "Europe/Rome", "Africa/Johannesburg",
            "Asia/Beijing", "Canada/Atlantic", "Canada/Central", "Canada/Eastern",
            "Canada/Pacific", "America/New_York", "America/Los_Angeles"
        };

        // Add all time zones to the list
        for (String timeZoneId : timeZoneIds) {
            TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
            String gmtOffset = getGmtOffsetString(timeZone);

            Map<String, Object> zone = new HashMap<>();
            zone.put(KEY_ID, timeZoneId);
            zone.put(KEY_NAME, timeZoneId.substring(timeZoneId.lastIndexOf('/') + 1).replace('_', ' '));
            zone.put(KEY_GMT, gmtOffset);

            zonesList.add(zone);
        }

        // Sort zones by GMT offset (ascending)
        Collections.sort(zonesList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                TimeZone tz1 = TimeZone.getTimeZone((String) o1.get(KEY_ID));
                TimeZone tz2 = TimeZone.getTimeZone((String) o2.get(KEY_ID));
                return Integer.compare(tz1.getRawOffset(), tz2.getRawOffset());
            }
        });

        // Move "Europe/London" to the top of the list for default selection
        for (int i = 0; i < zonesList.size(); i++) {
            if ("Europe/London".equals(zonesList.get(i).get(KEY_ID))) {
                Map<String, Object> londonZone = zonesList.remove(i);
                zonesList.add(0, londonZone); // Add London to the top
                break;
            }
        }

        return zonesList;
    }

    private static String getGmtOffsetString(TimeZone timeZone) {
        int offsetMillis = timeZone.getRawOffset();
        int hours = offsetMillis / (1000 * 60 * 60);
        int minutes = Math.abs((offsetMillis / (1000 * 60)) % 60);
        return String.format("GMT%+d:%02d", hours, minutes);
    }

    public static CharSequence getTimeZoneOffsetAndName(Context context, TimeZone tz, Date now) {
        return "ddd ddd";
    }
}
